package com.example.lancooznbk.view.adapter;

import android.animation.Animator;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.lancooznbk.R;
import com.example.lancooznbk.model.result.FavouriteListEntity;
import com.example.lancooznbk.utils.AnimatorHelper;
import com.example.lancooznbk.view.widget.RippleView;
import com.lancoo.reviewsystem.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;


public class FavouriteListAdapter extends BaseRecyclerAdapter<FavouriteListAdapter.FavouriteAdapterViewHolder> {

    private List<FavouriteListEntity.FavouriteListBean> list = new ArrayList<>();
    private final Activity context;
    private OnItemClickListener onItemClickListener;
    //private String[] typeByThree = new String[3];

    private int actionItem;

    private final Interpolator mInterpolator = new LinearInterpolator();
    private int mLastPosition = -1;


     public interface OnItemClickListener{
         void onItemClick(View view, int position);
         void onItemDel(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public FavouriteListAdapter(Activity context, List<FavouriteListEntity.FavouriteListBean> favouriteBeanList) {
        this.context = context;
        this.list = favouriteBeanList;
    }

    public FavouriteListAdapter(Activity context) {
        this.context = context;
        //typeByThree = context.getResources().getStringArray(R.array.QuesType_THREE_Arrays);
    }


    @Override
    public void onBindViewHolder(final FavouriteAdapterViewHolder holder, final int position, boolean isItem) {

        String itemTypeName = list.get(position).getTypeName().trim();
        holder.favouriteItem.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView v) {

                    if (onItemClickListener != null && list.size() != 0) {//修复数据清除了界面还存在点击的bug
                        actionItem = (Integer) holder.favouriteItem.getTag();
                        onItemClickListener.onItemClick(v,actionItem);
                    }
            }
        });
        holder.btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null && list.size() != 0) {
                    actionItem = (Integer) holder.favouriteItem.getTag();
                    onItemClickListener.onItemDel(holder.favouriteItem,actionItem);
                }
            }
        });
        holder.favouriteItem.setTag(position);


        FavouriteListEntity.FavouriteListBean treeInfoEntity = list.get(position);

        holder.treeItemSummary.setText(Html.fromHtml(treeInfoEntity.getQuesBrief()));

        //String startTime = TimeUtils.formateDateString(treeInfoEntity.getCreateTime(),"yyyy/MM/dd HH:mm:ss","yyyy-MM-dd");
        String startTimeStr = String.format(context.getString(R.string.favouriteTime),TimeUtils.getDate(treeInfoEntity.getCreateTime()));
        holder.favouriteTime.setText(startTimeStr);
        String  typeId = treeInfoEntity.getTypeID();
        if (TimeUtils.dateIsToday(treeInfoEntity.getCreateTime())){//今天
            holder.circleImage.setBackgroundResource(R.mipmap.bg_item_radio_wrong);
        }else  if (TimeUtils.dateIsYestoday(treeInfoEntity.getCreateTime())){//昨天
            holder.circleImage.setBackgroundResource(R.mipmap.bg_item_fill_wrong);
        }else{//以前
            holder.circleImage.setBackgroundResource(R.mipmap.bg_item_other_wrong);
        }

        String typeName = itemTypeName.substring(0,
                itemTypeName.length() > 4 ? 4 : itemTypeName.length() );
        StringBuilder itemTypeBuffer = new StringBuilder(typeName);
        if (itemTypeBuffer.length() > 2) {
            itemTypeBuffer.insert(2, "\n");
        }
        holder.circleImage.setText(itemTypeBuffer.toString());

        int adapterPosition = holder.getAdapterPosition();

        boolean isScrollDown = adapterPosition > mLastPosition;

        for (Animator anim : AnimatorHelper.getRecyclerAnimator(holder.itemView,isScrollDown)) {
            anim.start();
        }

    }





    @Override
    public int getAdapterItemCount() {
        return list.size();
    }

    public List<FavouriteListEntity.FavouriteListBean>getAdapterItemList(){
        return list;
    }
    @Override
    public FavouriteAdapterViewHolder getViewHolder(View view) {
        return new FavouriteAdapterViewHolder(view, false);
    }

    @Override
    public FavouriteAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType,boolean isItem) {
        View v;
            v = LayoutInflater.from(context).inflate(R.layout.item_favourite_list,parent,false);
            return new FavouriteAdapterViewHolder(v,true);
    }

    class FavouriteAdapterViewHolder extends RecyclerView.ViewHolder{
        TextView favouriteTime;
        RippleView favouriteItem;
        TextView circleImage;
        TextView treeItemSummary;

        Button btnDel;
        public FavouriteAdapterViewHolder(View itemView,boolean isItem) {
           super(itemView);
            if (isItem) {
                favouriteItem = (RippleView) itemView.findViewById(R.id.ripple_favourite_item);
                circleImage = (TextView) itemView.findViewById(R.id.img_favourite_title_summary);
                treeItemSummary = (TextView) itemView.findViewById(R.id.tv_favourite_summary);
                favouriteTime = (TextView) itemView.findViewById(R.id.tv_favourite_time);
                btnDel = (Button) itemView.findViewById(R.id.btnDelete);
            }

        }


    }



    //添加数据
    public void add(List<FavouriteListEntity.FavouriteListBean> favouriteBeanList){
            //int ss = getStart();
            int startPosition = getStart()+getAdapterItemCount();
           // int endPosition = startPosition+treeInfoEntityList.size()-1;
            list.addAll(favouriteBeanList);
            notifyItemRangeInserted(startPosition, favouriteBeanList.size());
    }

    public void clearAndAdd(List<FavouriteListEntity.FavouriteListBean> favouriteBeanList){
        list.clear();
        list.addAll(favouriteBeanList);
        notifyDataSetChanged();
        mLastPosition = -1;
    }




    public void remove(int position) {
        list.remove(position);
        remove(list, position);
    }

    public void remove(){
        list.remove(actionItem);
        //notifyDataSetChanged();
        notifyItemRemoved(actionItem);
        notifyItemRangeChanged(actionItem,getItemCount());


    }

    public void clear() {
       list.clear();//清空不刷新
        //clear(list);//清空刷新
    }


}
